/* eslint-disable handle-callback-err */
<template>
  <div class="app-container">
    <el-form :model="dataForm" class="cus_form">
      <el-row :gutter="20">
        <el-col :span="14">
          <el-form-item>
            <el-input v-model="dataForm.userName" size="mini" placeholder="用户名/邮箱/姓名" clearable>
              <el-button slot="append" icon="el-icon-search" @click="getDataList()" />
            </el-input>
          </el-form-item>
        </el-col>
        <el-col :span="10" style="text-align: right; ">
          <el-button icon="el-icon-plus" size="mini" class="cla-user-add" type="primary" @click="addOrUpdateHandle()">
            新建
          </el-button>&nbsp;&nbsp;&nbsp;&nbsp;
          <el-dropdown>
            <span class="el-dropdown-link">
              其他操作<i class="el-icon-arrow-down el-icon--right" />
            </span>
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item command="a">新增</el-dropdown-item>
              <el-dropdown-item command="b">删除</el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>
            &nbsp;&nbsp;
        </el-col>
      </el-row>
    </el-form>
    <tree-table
      :data="dataList"
      class="eltable"
      :columns="columns"
      row-key="Id"
      style="width: 100%;"
    >
      <el-table-column
        header-align="center"
        align="center"
        label="图标"
      >
        <template slot-scope="scope">
          <svg-icon :icon-class="scope.row.Icon" class-name="custom-class" />
          <!-- <svg-icon :name="scope.row.Icon || ''"></svg-icon> -->
        </template>
      </el-table-column>
      <el-table-column
        prop="Type"
        header-align="center"
        align="center"
        label="类型"
      >
        <template slot-scope="scope">
          <el-tag v-if="scope.row.Type === 0" size="small">目录</el-tag>
          <el-tag v-else-if="scope.row.Type === 1" size="small" type="success">菜单</el-tag>
          <el-tag v-else-if="scope.row.Type === 2" size="small" type="info">按钮</el-tag>
        </template>
      </el-table-column>
      <el-table-column
        prop="OrderNum"
        header-align="center"
        align="center"
        label="排序号"
      />
      <el-table-column
        prop="Url"
        header-align="center"
        align="left"
        width="150"
        :show-overflow-tooltip="true"
        label="菜单URL"
      />
      <el-table-column
        prop="Perms"
        header-align="center"
        align="center"
        width="150"
        :show-overflow-tooltip="true"
        label="授权标识"
      />
      <el-table-column
        fixed="right"
        header-align="center"
        align="center"
        width="150"
        label="操作"
      >
        <template slot-scope="scope">
          <el-button type="text" size="small" @click="addOrUpdateHandle(scope.row.Id)">
            <span class="font-12 iconfont myicon-xiugai" />&nbsp;修改
          </el-button>
          <el-button type="text" size="small" @click="deleteHandle(scope.row.Id)">
            <span class="font-12 iconfont myicon-shanchu" />&nbsp;删除
          </el-button>
        </template>
      </el-table-column>
    <!-- </el-table> -->
    </tree-table>
    <add-or-update v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getDataList" />
  </div>
</template>

<script>
// eslint-disable-next-line no-unused-vars
import { treeDataTranslate, treeToArrayList, getChildNodeFromArray } from '@/utils'
import { fetchList, deleteMenu } from '@/api/menu'
import treeTable from '@/components/TreeTable'
import AddOrUpdate from './menu-add-or-update'
export default {
  components: {
    treeTable,
    AddOrUpdate
  },
  data() {
    return {
      documentClientHeight: this.$store.state.common.documentClientHeight - 250,
      dataForm: {},
      dataList: [],
      dataListLoading: false,
      addOrUpdateVisible: false,
      columns: [{
        text: '名称',
        value: 'Name',
        width: 100,
        attrs: {},
        events: {}
      }, {
        text: '上级菜单',
        value: 'ParentName',
        width: 100,
        attrs: {},
        events: {}
      }]
    }
  },
  mounted() {
    this.getDataList()
  },
  methods: {
    // 获取数据列表
    getDataList() {
      this.dataListLoading = true
      fetchList({}).then(({ data }) => {
        this.dataList = treeDataTranslate(data, 'Id')
        this.dataListLoading = false
      // eslint-disable-next-line handle-callback-err
      }).catch((error) => {
        this.dataListLoading = false
      })
    },
    // 新增 / 修改
    addOrUpdateHandle(id) {
      this.addOrUpdateVisible = true
      this.$nextTick(() => {
        this.$refs.addOrUpdate.init(id)
      })
    },
    // 删除
    deleteHandle(id) {
      this.$confirm(`确定对[id=${id}]进行[删除]操作?`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        deleteMenu(id).then((data) => {
          if (data && data.code === '20000') {
            this.$message({
              message: '操作成功',
              type: 'success',
              duration: 1500,
              onClose: () => {
                this.getDataList()
              }
            })
          } else {
            this.$message.error(data.msg)
          }
        })
      }).catch(() => {})
    }
  }
}
</script>>
